<template>
  <!-- 卡片列表 -->
  <div class="app-container">
    <div class="filter-container">
      <el-form @submit.native.prevent>
        <el-date-picker
          v-model="listQuery.time"
          type="daterange"
          range-separator="至"
          start-placeholder="下单开始日期"
          end-placeholder="下单结束日期"
        />
        <el-input placeholder="微信订单号" style="width: 200px" class="filter-item" v-model="listQuery.wx_id" />
        <el-input placeholder="商户订单号" style="width: 200px" class="filter-item" v-model="listQuery.id" />

        <el-select style="width: 200px" v-model="listQuery.status" placeholder="支付状态">
          <el-option
            v-for="(item, index) in [
              { id: -1, value: '全部' },
              { id: 1, value: '已支付' },
              { id: 0, value: '未支付' },
              { id: 2, value: '已退款' },
            ]"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          native-type="submit"
          @click="fetchData(1)"
          >查询</el-button
        >
      </el-form>
    </div>
    <!-- 表格内容 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <!-- <el-table-column align="center" type="selection" width="70" /> -->

      <el-table-column label="微信支付订单号" align="center">
        <template slot-scope="scope">{{ scope.row.wx_id }}</template>
      </el-table-column>
      <el-table-column label="商户订单号" align="center">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="卡号码" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="$router.push(`/card/info/${scope.row.card.id}`)">{{
            scope.row.card.business_code
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="ICCID" align="center">
        <template slot-scope="scope">{{ scope.row.card.iccid }}</template>
      </el-table-column>
      <el-table-column label="套餐/加油包" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.meal">{{ scope.row.meal.name }}</span>
          <span v-if="scope.row.package">{{ scope.row.package.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center">
        <template slot-scope="scope">{{ scope.row.total_fee }}</template>
      </el-table-column>
      <el-table-column label="支付状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">已支付</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">已退款</el-tag>
          <el-tag v-if="!scope.row.status" type="info">未支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
      <el-table-column label="支付时间" align="center">
        <template slot-scope="scope">{{ scope.row.pay_time }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="
              dialogVisible = true;
              row = { ...scope.row };
            "
            v-show="scope.row.status == 1"
            >退款</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="fetchData"
    />
    <el-dialog title="退款金额" :visible.sync="dialogVisible" width="30%">
      <div><el-input v-model="row.total_fee"></el-input></div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleRevert">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'; // waves directive
import Pagination from '@/components/Pagination'; // secondary package based on el-pagination
import { getList } from '@/api/order';
import { revert } from '@/api/order';
import dayjs from 'dayjs';

export default {
  directives: {
    waves,
  },
  components: {
    Pagination,
  },
  data() {
    return {
      dialogVisible: false,
      row: {},

      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1, // 当前页码
        size: 50, // 每页大小

        time: this.$route.query.time
          ? [
              new Date(Number(this.$route.query.time.split(',')[0])),
              new Date(Number(this.$route.query.time.split(',')[1])),
            ]
          : null,

        status: this.$route.query.status ? Number(this.$route.query.status) : null,
      },
    };
  },
  computed: {},
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const res = await getList({
        ...this.listQuery,
        createTimeStart: this.listQuery.time && dayjs(this.listQuery.time[0]).format('YYYY-MM-DD HH:mm:ss'),
        createTimeEnd: this.listQuery.time && dayjs(this.listQuery.time[1]).format('YYYY-MM-DD HH:mm:ss'),
      });
      this.total = res.data.total;
      this.list = res.data.list;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async handleRevert() {
      this.dialogVisible = false;
      await revert({ order_id: this.row.id, refund_fee: Number(this.row.total_fee) });

      this.$message({
        type: 'success',
        message: '退款成功!',
      });
      this.fetchData();
    },
  },
};
</script>
<style scoped lang="scss">
.btn {
  // margin: 5px;
}

.filter-container {
  .filter-item {
    margin-bottom: 10px;
  }
}
</style>
